package person.bing.fbcloud.controller;


import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import person.bing.fbcloud.api.dto.PostCategoryDTO;
import person.bing.fbcloud.common.core.exception.CheckedException;
import person.bing.fbcloud.common.core.util.R;
import person.bing.fbcloud.service.PostCategoryService;

/**
 * <p>
 *  帖子类型
 * </p>
 *
 * @author mengfb
 * @since 2019-06-24
 */
@RestController
@RequestMapping("/postcategory")
public class PostCategoryController {
    @Autowired
    private PostCategoryService postCategoryService;

    /**
     * 添加帖子类型
     * @param postCategoryDTO
     * @return
     */
    @PostMapping
    public R addCategory(@RequestBody PostCategoryDTO postCategoryDTO){
        String categoryKey = postCategoryDTO.getCategoryKey();
        String categoryDesc = postCategoryDTO.getCategoryDesc();
        if (StrUtil.isEmpty(categoryKey)){
            throw new CheckedException("categoryKey不能为空");
        }
        if (StrUtil.isEmpty(categoryDesc)){
            throw new CheckedException("categoryDesc不能为空");
        }
        postCategoryService.addCategory(categoryKey,categoryDesc);
        return new R();
    }

    /**
     * 获取帖子类型列表
     * @return
     */
    @GetMapping
    public R getCategory(){
        return new R(postCategoryService.list());
    }

}
